using Peak.Can.Basic;

namespace MEA.AE_Data
{
	public class CANMessage
	{
		public delegate void OnMessageSend(object sender);

		public delegate void OnMessageReceive(object sender);

		public static readonly string _Version = "0300";

		private TPCANMsg rMsg;

		private bool rbo_Changed;

		private bool rbo_Received;

		private bool rbo_ReceiveTimedout;

		private bool rbo_Sent;

		private byte rby_Chip;

		private string rst_Name;

		private uint rui_ReceiveTimeout;

		private uint rui_ReceiveTimestamp;

		private uint rui_SendTimeout;

		private uint rui_SendTimestamp;

		public bool Changed
		{
			get
			{
				return rbo_Changed;
			}
			set
			{
				rbo_Changed = value;
			}
		}

		public byte Chip
		{
			get
			{
				return rby_Chip;
			}
			set
			{
				rby_Chip = value;
			}
		}

		public byte DataLength
		{
			get
			{
				return rMsg.LEN;
			}
			set
			{
				if (value > 8)
				{
					rMsg.LEN = 8;
				}
				else
				{
					rMsg.LEN = value;
				}
			}
		}

		public byte[] Data
		{
			get
			{
				return rMsg.DATA;
			}
			set
			{
				if (value.Length == rMsg.LEN)
				{
					rMsg.DATA = value;
				}
			}
		}

		public TPCANMsg Msg
		{
			get
			{
				return rMsg;
			}
			set
			{
				rMsg = value;
			}
		}

		public uint ID
		{
			get
			{
				return rMsg.ID;
			}
		}

		public string Name
		{
			get
			{
				return rst_Name;
			}
		}

		public bool Received
		{
			get
			{
				return rbo_Received;
			}
			set
			{
				rbo_Received = value;
				if (rbo_Received && this.MessageReceive != null)
				{
					this.MessageReceive(this);
				}
			}
		}

		public bool ReceiveTimedout
		{
			get
			{
				return rbo_ReceiveTimedout;
			}
			set
			{
				rbo_ReceiveTimedout = value;
			}
		}

		public uint ReceiveTimeout
		{
			get
			{
				return rui_ReceiveTimeout;
			}
			set
			{
				rui_ReceiveTimeout = value;
			}
		}

		public uint ReceiveTimestamp
		{
			get
			{
				return rui_ReceiveTimestamp;
			}
			set
			{
				rui_ReceiveTimestamp = value;
			}
		}

		public uint SendTimeout
		{
			get
			{
				return rui_SendTimeout;
			}
			set
			{
				rui_SendTimeout = value;
			}
		}

		public uint SendTimestamp
		{
			get
			{
				return rui_SendTimestamp;
			}
			set
			{
				rui_SendTimestamp = value;
			}
		}

		public bool Sent
		{
			get
			{
				return rbo_Sent;
			}
			set
			{
				rbo_Sent = value;
			}
		}

		public byte this[int i]
		{
			get
			{
				i &= 7;
				return rMsg.DATA[i];
			}
			set
			{
				i &= 7;
				if (rMsg.DATA[i] != value)
				{
					rMsg.DATA[i] = value;
					rbo_Changed = true;
				}
			}
		}

		public byte this[uint i]
		{
			get
			{
				i &= 7;
				return rMsg.DATA[i];
			}
			set
			{
				i &= 7;
				if (rMsg.DATA[i] != value)
				{
					rMsg.DATA[i] = value;
					rbo_Changed = true;
				}
			}
		}

		public TPCANMessageType MsgType
		{
			get
			{
				return rMsg.MSGTYPE;
			}
			set
			{
				rMsg.MSGTYPE = value;
			}
		}

		public event OnMessageSend MessageSend;

		public event OnMessageReceive MessageReceive;

		public CANMessage(uint MessageID, uint SendTimeout, uint ReceiveTimeout, byte DataLength, uint xtd, string Name, byte Chip)
		{
			rMsg = default(TPCANMsg);
			rMsg.ID = MessageID;
			rMsg.LEN = DataLength;
			rMsg.DATA = new byte[DataLength];
			if (xtd == 0)
			{
				rMsg.MSGTYPE = TPCANMessageType.PCAN_MESSAGE_STANDARD;
			}
			else
			{
				rMsg.MSGTYPE = TPCANMessageType.PCAN_MESSAGE_EXTENDED;
			}
			rbo_Changed = false;
			rbo_Received = false;
			rbo_ReceiveTimedout = true;
			rbo_Sent = false;
			rby_Chip = Chip;
			rst_Name = Name;
			rui_ReceiveTimeout = ReceiveTimeout;
			rui_ReceiveTimestamp = 0u;
			rui_SendTimeout = SendTimeout;
			rui_SendTimestamp = 0u;
		}

		public CANMessage(ref TPCANMsg tpCANMsg)
		{
			rMsg = tpCANMsg;
			rbo_Changed = false;
			rbo_Received = false;
			rbo_ReceiveTimedout = true;
			rbo_Sent = false;
			rby_Chip = Chip;
			rst_Name = Name;
			rui_ReceiveTimeout = ReceiveTimeout;
			rui_ReceiveTimestamp = 0u;
			rui_SendTimeout = SendTimeout;
			rui_SendTimestamp = 0u;
		}

		public ulong DataGet(byte Start, byte Length)
		{
			ulong num = DataGetLong();
			num <<= 64 - Start - Length;
			return num >> 64 - Length;
		}

		private ulong DataGetLong()
		{
			ulong num = 0uL;
			for (int i = 0; i < 8; i++)
			{
				num += (ulong)rMsg.DATA[i] << 8 * i;
			}
			return num;
		}

		public void FireMessageSend()
		{
			if (this.MessageSend != null)
			{
				this.MessageSend(this);
			}
		}

		public void UpDateTPCANMsg(ref CANMessage lMessage)
		{
			rMsg = lMessage.rMsg;
		}
	}
}
